<%@ page language="java" pageEncoding="us-ascii" contentType="text/html;charset=us-ascii" %>
<%--Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
	 
	This file is part of CDinFusion.
	
	CDinFusion is free software: you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public 
	License in version 3 (LGPL3) as published by the Free Software
	Foundation version 3 of the License.
	
	CDinFusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	Lesser General Public License for more details.
	
	You should have received a copy of the Lesser General Public
	License along with CDinFusion; see the file COPYING. If not,
	write to the Free Software Foundation, 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
	
	If you have any questions, send an email to: megx@mpi-bremen.de.
--%>
<%@ page import="org.cdinfusion.data.*"%>
<%@ page import="org.cdinfusion.data.studytype.*"%>
<%@ page import="org.cdinfusion.data.environment.*"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="java.util.Iterator" %>


<%System.out.println("<<<<<<<<<<3b1_CD_INPUT_ntom>>>>>>>>>>");
String origin = session.getAttribute("origin").toString();
String initial = "true";
String webinitial ="empty";
session.setAttribute("initial", initial);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
</style>
<title>DIRECT CONTEXTUAL DATA INPUT n TO m</title> 
<jsp:include page="googleanalytics.jsp"/>

<script type="text/javascript">
function openpopup(url){
	fenster=window.open(url, "_blank", "width=600,height=400,status=yes, scrollbars=yes, resizable=yes, toolbar=1, menubar=1, location=1");
	fenster.focus();
	return false;
}
function select() {
/*num of seqIDs compare to 10 -> is somthing sesected?*/
var temp = 1;
var seq_num = 10;
var seqID_num = document.Formular.size.value - 1;
to = document.Formular.ToSeqID.value;
regexp = /.+/;
if(!(to.match(regexp))){
	alert("Please select a 'to' sequence ID!");
	document.Formular.ToSeqID.focus();
	return false;
} 

var num = 1;
/*study = none & env = none*/
var result = document.Formular.StudyTypeDropDown.value.match("none") && document.Formular.EnvParamDropDown.value.match("none");
if(result){
  	num = -1;
}
if(num == -1){
	alert("Please select a type of report and a environmental package!");
    document.Formular.StudyTypeDropDown.focus();
    return false;
}
/*study = none*/
result = document.Formular.StudyTypeDropDown.value.match("none");
if(result){
  	num = -1;
}
if(num == -1){
	alert("Please select a type of report!");
    document.Formular.StudyTypeDropDown.focus();
    return false;
}
/*env = none*/
result = document.Formular.EnvParamDropDown.value.match("none");
if(result){
  	num = -1;
}
if(num == -1){
	alert("Please select a environmental package!");
    document.Formular.EnvParamDropDown.focus();
    return false;
}
return true;
}/*end function*/
</script>
</head>

<body>
<table border="0" width="100%" bgcolor="#2B3990" cellpadding=0 cellspacing=0>
<tr bgcolor="#ffffff">
<td width=250>
<center>
<img height="81" width="250" alt="CDinFusion Logo" src="/CDinFusion/img/logo_small_dark.png">
</center>
</td>
</tr>

<tr align="left">    
<td>
<jsp:include page="documentation/navigation.jsp"></jsp:include>
</td>
</tr>

<%
String sessionID = request.getParameter("sessionID");

List<String> allSequenceIDs = (List<String>) session.getAttribute("allSequenceIDs");
List<String> remainingSequenceIDs = new ArrayList<String>();
List<String> currentlySelectedIDs = new ArrayList<String>();

boolean confirm_exist = false;
boolean first = false;
boolean not_equal = false;
String studytype = "";
String envtype = "";
List<String> sSeqID = new ArrayList<String>(); //remove later
List<String> uSeqID = new ArrayList<String>(); //remove later
List<String> old_usedSequenceIDs = new ArrayList<String>();
List<String> usedSequenceIDs = new ArrayList<String>();

int seq_num = 10;
int size = allSequenceIDs.size();
int height = 10;//height of seqID box
String fromID = "";
String toID = "";

if(origin.equals("2b")){
	first = true;
	webinitial = "true";
	session.setAttribute("webinitial", webinitial);
	String second_step = "false";
	session.setAttribute("second_step", second_step);
	remainingSequenceIDs = allSequenceIDs;     
}

List<CDElement> cdeLIST = null;

if(origin.equals("3b2")){
	String second_step = "true";
	session.setAttribute("second_step", second_step);
	
	usedSequenceIDs = (List<String>) session.getAttribute("usedSequenceIDs");
	
	String branch = (String) session.getAttribute("branch");
	webinitial = (String) session.getAttribute("webinitial");
	if(webinitial.equals("false") & !branch.equals("webform")){
		// MAYBE change back here ==> 
		old_usedSequenceIDs = (List<String>) session.getAttribute("old_usedSequenceIDs");
		for(int i = 0; i < usedSequenceIDs.size(); i++){
			 old_usedSequenceIDs.add(usedSequenceIDs.get(i));
		}
		usedSequenceIDs = old_usedSequenceIDs;
	}
	// <== MAYBE change back here 
	//uSeqID = (List<String>) request.getAttribute("uSeqID");  //remove later
	//session.setAttribute("uSeqID", uSeqID);  //remove later
	//aSeqID.clear();  //remove later
//	aSeqID = (List<String>) request.getAttribute("aSeqID");  //remove later
	
	//Set operations to calculate remaining sequence IDs
	Set<String> all = new HashSet<String>(allSequenceIDs);
	Set<String> used = new HashSet<String>(usedSequenceIDs);
	Set<String> remaining = new HashSet<String>(allSequenceIDs);
	if (used.size()==1){
		Set<String> tempSet = new HashSet<String>();
		
		for(int i = 0; i < allSequenceIDs.size(); i++){
			if(!allSequenceIDs.get(i).trim().equals(usedSequenceIDs.get(0).trim())){
				tempSet.add(allSequenceIDs.get(i));
			}
		}
		remaining = tempSet;
	}else{
		remaining.removeAll(used);
	}
	Iterator<String> itr = remaining.iterator();
	
	remainingSequenceIDs = new ArrayList<String>(remaining.size());
	String element = "";
	
	String[] temp = new String[remaining.size()];

	for (int i = 0; i < allSequenceIDs.size(); i++){
		itr = remaining.iterator();
		while(itr.hasNext()){
			element = itr.next();

			if (element.equals(allSequenceIDs.get(i))|allSequenceIDs.get(i).equals(element)){
				remainingSequenceIDs.add(i-used.size(), element);//TODO: so the upload of filled csv has to be in order! 
				temp[i-used.size()] = element;
			}
		}
	}

	session.setAttribute("old_usedSequenceIDs", usedSequenceIDs);
	initial = "false";
	session.setAttribute("initial", initial);
}
if(origin.equals("3b1_csv")){
	String second_step = (String) session.getAttribute("second_step");
	currentlySelectedIDs = (List<String>) session.getAttribute("currentlySelectedSIDs");

	cdeLIST = (ArrayList<CDElement>) session.getAttribute("cdeLIST");
	studytype = cdeLIST.get(cdeLIST.size()-1).getStudy().getClass().toString().toLowerCase().substring(36).trim();
	envtype = cdeLIST.get(cdeLIST.size()-1).getEnv().getClass().toString().toLowerCase().substring(38).trim();
	if(second_step.equals("false")){
		remainingSequenceIDs = allSequenceIDs;
	}else{//(second_step.equals("true"))
		usedSequenceIDs = (List<String>) session.getAttribute("usedSequenceIDs");

		Set<String> all = new HashSet<String>(allSequenceIDs);
		Set<String> used = new HashSet<String>(usedSequenceIDs);
		Set<String> remaining = new HashSet<String>(allSequenceIDs);
		Set<String> current = new HashSet<String>(currentlySelectedIDs);
		if (used.size() == 1){
			Set<String> tempSet = new HashSet<String>();
			
			for(int i = 0; i < allSequenceIDs.size(); i++){
				if(!allSequenceIDs.get(i).trim().equals(usedSequenceIDs.get(0).trim())){
					tempSet.add(allSequenceIDs.get(i));
				}
			}
			remaining = tempSet;
		} else {
			remaining.removeAll(used);
		}
		Iterator<String> itr = remaining.iterator();
		
		remainingSequenceIDs = new ArrayList<String>(remaining.size());
		String element = "";
		
		String[] temp = new String[remaining.size()];
		
		for(int i = 0; i < allSequenceIDs.size(); i++){
			itr = remaining.iterator();
			while(itr.hasNext()){
				element = itr.next();
				if(element.equals(allSequenceIDs.get(i))|allSequenceIDs.get(i).equals(element)){
					remainingSequenceIDs.add(i-used.size(), element);//here and
					temp[i-used.size()] = element;//here maybe : +current
				}//end if(element.equals...
			}//end while
		}//end for (i)
	}//end else (second_step.equals("true"))
}
%>

<tr bgcolor="#ffffff">
<td>
<div class="center_div">    
<br>
<center><b> Specify GSC compliant contextual data:</b> <a href="gsc_package_info.htm?item=general" target="_blank" onclick="openpopup(this.href); return false"> more info </a><br>
Chose type of report and environmental package: <br>
</center>
<center>			
<form action="upload.htm?_flowId=fileupload&amp;sessionID=<%=sessionID%>" id="fileupload" name="fileupload" method="POST" >
	<input type="hidden" name="branch" value="csvupload">
	<%
	session.setAttribute("origin", "3b1");
	%>
	<p> If you have your filled csv file, you can upload it now:
	<input class="submitbutton" style="width: 175px" type="submit" name="Submit" value="upload" id="upload">
	</p>
</form>


<form name="Formular" action="GSC_choice_c.htm" method="post" onsubmit="return select()">
	<input type="hidden" id="size" name="size" value="<%=size%>">
	<%
	session.setAttribute("origin", "3b1");
	%>
	sequence identifier:
	
	<%
		if(origin.equals("3b2")|origin.equals("2b")){
		//DROPDOWN BOX FROM AND TO SEQID%>
				<table>
					<tr>
						<td>from:</td>
						<td>
							<strong><%=remainingSequenceIDs.get(0)%></strong>
							<input type="hidden" id="FromSeqID" name="FromSeqID" value="<%=remainingSequenceIDs.get(0)%>">
						</td>
					</tr>
					<tr>
						<td valign="top">to:</td>
						<td>
							<select name="ToSeqID" size="<%=height%>">
								<%for(int i = 0; i < remainingSequenceIDs.size(); i++){
									if(i == 0){%>
										<option selected value="<%=remainingSequenceIDs.get(i)%>"><%=i+1%> <%=remainingSequenceIDs.get(i)%></option>
									<%}else{%>
										<option value="<%=remainingSequenceIDs.get(i)%>"><%=i+1%> <%=remainingSequenceIDs.get(i)%></option>
									<%}%>
				     			<%}%>
							</select>
						</td>
					</tr>
				</table>
			<!--	</p>--><%
			//end else	
		}//end if(origin.equals("3b2")|origin.equals("2b"))
	
		if(origin.equals("3b1_csv")|origin.equals("5")){
			//DROPDOWN BOX FROM AND TO SEQID
				%>
				<table>
					<tr>
						<td>from:</td>
						<td>
							<strong><%=currentlySelectedIDs.get(0)%></strong>
							<input type="hidden" id="FromSeqID" name="FromSeqID" value="<%=currentlySelectedIDs.get(0)%>">
						</td>
					</tr>
					<tr>
						<td valign="top">to:</td>
						<td>
							<select name="ToSeqID" size="<%=height%>">
								<%for(int i = 0; i < remainingSequenceIDs.size(); i++){
									if(currentlySelectedIDs.get(currentlySelectedIDs.size()-1).equals(remainingSequenceIDs.get(i))){%>
										<option value="<%=remainingSequenceIDs.get(i)%>" selected><%=i+1%> <%=remainingSequenceIDs.get(i)%></option>
									<%}else{
									%>
				     					<option value="<%=remainingSequenceIDs.get(i)%>"><%=i+1%> <%=remainingSequenceIDs.get(i)%></option>
				     				<%}
				     			}%>
							</select>
						</td>
					</tr>
				</table>
				<!-- </p> --><%
		}//end if(origin.equals("3b1_csv")|origin.equals("5"))
		%>
	<br><br>
	
		<p>
		type of report:
		<select name="StudyTypeDropDown" size="1"> 
     		<option value="none">none selected</option>
   			<%
   			if(studytype.contains("mimarks_s")){%> 
   				<option selected value="mimarks_s">MIMARKS survey (mimarks_s)</option>
   			<%}
   			else{%>
   				<option value="mimarks_s">MIMARKS survey (mimarks_s)</option>
   			<%}
   			if(studytype.contains("mimarks_c")){%>
   				<option selected value="mimarks_c">MIMARKS culture (mimarks_c)</option>
   			<%}
   			else{%>
   				<option value="mimarks_c">MIMARKS culture (mimarks_c)</option>
   			<%}
   			if(studytype.contains("eu")){%>
   				<option selected value="eu">MIGS eukaryotes (eu)</option>
   			<%}
   			else{%>
   				<option value="eu">MIGS eukaryotes (eu)</option>
   			<%}
   			if(studytype.contains("ba")){%>
   				<option selected value="ba">MIGS bacteria and archaea (ba)</option>
   			<%}
   			else{%>
   				<option value="ba">MIGS bacteria and archaea (ba)</option>  
   			<%}
   			if(studytype.contains("pl")){%>
   				<option selected value="pl">MIGS plasmid (pl)</option>
			<%}
   			else{%>
   				<option value="pl">MIGS plasmid (pl)</option>
  			<%}
   			if(studytype.contains("vi")){%>
   				<option selected value="vi">MIGS virus (vi)</option>
  			<%}
   			else{%>
  				<option value="vi">MIGS virus (vi)</option>
   			<%}
   			if(studytype.contains("org")){%>
   				<option selected value="org">MIGS organelle (org)</option>
   			<%}
   			else{%>
   				<option value="org">MIGS organelle (org)</option>
   			<%}
   			if(studytype.contains("me")){%>
   				<option selected value="me">MIMS metagenome (me)</option>
   			<%}
   			else{%>
   				<option value="me">MIMS metagenome (me)</option>
   			<%}%>
   			</select>
			<a href="gsc_package_info.htm?item=typeofreport" target="_blank" onclick="openpopup(this.href); return false"> more info </a></p>
			
		<p><style="margin-left: 220">
		environmental package:
		<select name="EnvParamDropDown" size="1"> 
    		<option value="none">none selected</option>
   			<%
   			if(envtype.contains("water")){%>
   				<option selected value="water">water</option>
   			<%}
   			else{%>
   				<option value="water">water</option>
   			<%}
   			if(envtype.contains("wastewater")){%>
   				<option selected value="wastewater">wastewater/sludge</option>
   			<%}
   			else{%>
   				<option value="wastewater">wastewater/sludge</option>
   			<%}
   			if(envtype.contains("soil")){%>
   				<option selected value="soil">soil</option>
   			<%}
   			else{%>
   				<option value="soil">soil</option>
   			<%}
   			if(envtype.contains("sediment")){%>
   				<option selected value="sediment">sediment</option>
   			<%}
   			else{%>
   				<option value="sediment">sediment</option>
   			<%}
   			if(envtype.contains("plant_associated")){%>
   				<option selected value="plant_associated">plant-associated</option>
   			<%}
   			else{%>
   				<option value="plant_associated">plant-associated</option>
   			<%}
   			if(envtype.contains("miscellaneous")){%>
   				<option selected value="miscellaneous">miscellaneous</option>
   			<%}
   			else{%>
   				<option value="miscellaneous">miscellaneous</option>
   			<%}
   			if(envtype.contains("biofilm")){%>
   				<option selected value="biofilm">microbial mat/biofilm</option>
   			<%}
   			else{%>
   				<option value="biofilm">microbial mat/biofilm</option>
   			<%}
   			if(envtype.contains("human_vaginal")){%>
   				<option selected value="human_vaginal">human-vaginal</option>
   			<%}
   			else{%>
   				<option value="human_vaginal">human-vaginal</option>
   			<%}
   			if(envtype.contains("human_skin")){%>
   				<option selected value="human_skin">human-skin</option>
   			<%}
   			else{%>
   				<option value="human_skin">human-skin</option>
   			<%}
   			if(envtype.contains("human_oral")){%>
   				<option selected value="human_oral">human-oral</option>
   			<%}
   			else{%>
   				<option value="human_oral">human-oral</option>
   			<%}
   			if(envtype.contains("human_gut")){%>
   				<option selected value="human_gut">human-gut</option>
   			<%}
   			else{%>
   				<option value="human_gut">human-gut</option>
   			<%}
   			if(envtype.contains("human_associated")){%>
   				<option selected value="human_associated">human-associated</option>
   			<%}
   			else{%>
   				<option value="human_associated">human-associated</option>
   			<%}
   			if(envtype.contains("host_associated")){%>
   				<option selected value="host_associated">host-associated</option>
   			<%}
   			else{%>
   				<option value="host_associated">host-associated</option>
   			<%}
   			if(envtype.contains("air")){%>
   				<option selected value="air">air</option>
   			<%}
   			else{%>
   				<option value="air">air</option>
   			<%}%>
   			</select>
    		<a href="gsc_package_info.htm?item=environmentalpackage" target="_blank" onclick="openpopup(this.href); return false"> more info </a></p>
    <br>
	<input type="hidden" name="first" value="<%=first%>">
	<input type="hidden" name="branch" value="webform">	
	<input type="hidden" name="confirm_exist" value="<%=confirm_exist%>">

	<p> current selection as empty spreadsheet template <input class="submitbutton" type="submit" name="submit" value="download empty csv"></p>
	<input class="submitbutton" type="submit" name="submit" value="proceed">
</form>
</center>
</div>
</td>
</tr>
<tr bgcolor="#ffffff"><td align="right" height=30>
	<img height="20" width="200" alt="CDinFusion Step 2" src="/CDinFusion/img/step2.png">
</td></tr>
<jsp:include page="documentation/bottom_info.jsp"></jsp:include>
</table>
</body>
</html>